package com.example.bigevent.common.utils;



import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;




public class JWTUtil {

    public static String createJJWT(String secretKey, long ttl , Map<String,Object>claim){
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        long expMill=System.currentTimeMillis()+ttl;
        Date exp = new Date(expMill);
        JwtBuilder builder = Jwts.builder()
                .addClaims(claim)
                .signWith(signatureAlgorithm,secretKey.getBytes(StandardCharsets.UTF_8))
                .setExpiration(exp);

        return builder.compact();
    }

    public static Claims parserJJWT(String token,String secretKey){
        return Jwts.parser()
                .setSigningKey(secretKey.getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token)
                .getBody();
    }

    public static String creatJavaJWT(String secretKey,long ttl,String claimName,Map<String,Object>claim){
        long expMill =System.currentTimeMillis()+ttl;
        Date exp = new Date(expMill);
        return JWT.create()
                .withClaim(claimName,claim)
                .withExpiresAt(exp)
                .sign(Algorithm.HMAC256(secretKey));
    }


}
